package com.gxuwz.zlc.controller;

import com.gxuwz.zlc.common.Result;
import com.gxuwz.zlc.entity.Apply;
import com.gxuwz.zlc.entity.Audition;
import com.gxuwz.zlc.service.ApplyService;
import com.gxuwz.zlc.service.AuditionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 面试信息
 */
@Controller
@RequestMapping("/audition")
public class AuditionController {

    @Autowired
    private AuditionService auditionService;

    @Autowired
    private ApplyService applyService;

    // 查询所有
    @RequestMapping("/findAll")
    @ResponseBody
    public List<Audition> findAudition(){
        List<Audition> auditionList = auditionService.findAudition();
        return auditionList;
    }

    // 添加
    @RequestMapping("/add")
    @ResponseBody
    public Map addAudition(@RequestBody Audition audition){
        Map<String,String> map = new HashMap<>();
        try {
            auditionService.addAudition(audition);
            map.put("code","200");
            map.put("msg","添加成功");
        } catch (Exception e) {
            map.put("code","500");
            map.put("msg","添加失败");
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 实验技术人员
     * 提交面试成绩和录取状态(面试表)、在提交到报名表中
     * @param audition
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public Map updateAudition(@RequestBody Audition audition){
        // 提交到面试表中
        auditionService.updateAudition(audition);
        // 提交到报名表中
        Apply apply = new Apply();
        //apply.setStuId(audition.getStuId());
        apply.setInterviewResults(audition.getInterviewResults());
        apply.setIsSuccess(audition.getIsSuccess());
        applyService.updateApplyByStuId(apply,audition.getStuId());

        Map<String,String> map = new HashMap<>();
        map.put("code","200");
        map.put("msg","修改成功");
        return map;
    }

    // 删除
    @RequestMapping("/delete")
    @ResponseBody
    public Map deleteAudition(@RequestParam(name = "applyId") Integer auditionId){
       auditionService.deleteAudition(auditionId);
        Map<String,String> map = new HashMap<>();
        map.put("code","200");
        map.put("mag","删除成功");
        return map;
    }

    /**
     * 查询一个
     * @param auditionId
     * @return
     */
    @GetMapping("/findOne/{auditionId}")
    @ResponseBody
    public Result get(@PathVariable Integer auditionId){
        Audition auditionById = auditionService.findAuditionById(auditionId);
        // code=0,msg=成功
        return Result.success(auditionById);
    }

}
